const app = new getApp();
var wxParse = require('../../../../wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    register_type: 1, //注册方式
    copy_right: {
      is_load: -1
    },
    username: '', //用户名
    password: '', //密码
    confirm_password: '', //确认密码
    code: '', //验证码图
    verify_code: '', //验证码
    mobile: '', //手机号
    out_code: '', //动态验证码
    second: 0, //计时
    code_config: {
      value: {}
    }, //验证码配置
    reg_config: {}, //注册配置
    is_read_protocol: 0, // 注册协议是否已读
    is_show_protocol: 0, // 是否显示注册协议
    swiperHeight: 150,
    registerFlag: 0,
    getOutCodeFlag: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;

    app.copyRightLoad(that);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    app.restStatus(that, 'registerFlag');
    app.restStatus(that, 'getOutCodeFlag');

    // 加载注册配置
    app.sendRequest({
      url: 'System.Login.registerConfig',
      data: {},
      success: function(res) {
        let code = res.code;
        let data = res.data;
        console.log(res);
        if (code == 0) {
          let code_config = data.code_config;
          let reg_config = data.reg_config;

          // 是否开放注册
          if (reg_config.is_register == 0) {
            app.showBox('抱歉,商城暂未开放注册!');
            wx.switchTab({
              url: '/pages/index/index',
            })
          }

          // 验证码获取
          if (code_config.value.pc == 1) {
            app.verificationCode(that);
          }

          // 检测注册类型
          reg_config.register_info = app.checkEmpty(reg_config.register_info, '');
          reg_config.is_open_plain = reg_config.register_info.indexOf('plain') == -1 ? 0 : 1;
          reg_config.is_open_mobile = reg_config.register_info.indexOf('mobile') == -1 ? 0 : 1;

          if (reg_config.is_open_mobile == 0 && reg_config.is_open_plain == 0) {
            app.showBox('抱歉,商城暂未开放注册!');
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
          let register_type = 2;
          if (reg_config.is_open_plain == 1) register_type = 1;

          that.setData({
            code_config: code_config,
            reg_config: reg_config,
            register_type: register_type
          })
        }
      }
    });
    // 加载消息配置
    app.noticeConfig(that);
    // 加载注册协议
    that.loadProtocol();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 获取注册协议
   */
  loadProtocol: function() {
    let that = this;
    app.sendRequest({
      url: 'System.Login.registerAgreement',
      data: {},
      success: function(res) {
        let code = res.code;
        let data = res.data;
        console.log(res);
        if (code == 0) {
          let protocol = app.checkEmpty(data, {});
          //富文本格式转化
          let protocol_content = app.checkEmpty(protocol.content, '');
          wxParse.wxParse('protocol_content', 'html', protocol_content, that, 5);
          that.setData({
            protocol: protocol
          })
        }
      }
    });
  },

  /**
   * 显示注册协议
   */
  showProtocol: function() {
    let is_show_protocol = this.data.is_show_protocol;
    is_show_protocol = is_show_protocol == 1 ? 0 : 1;

    this.setData({
      is_show_protocol: is_show_protocol
    })
  },

  /**
   * 输入账号
   */
  inputUsername: function(e) {
    let username = e.detail.value;

    this.setData({
      username: username
    })
  },

  /**
   * 输入密码
   */
  inputPassword: function(e) {
    let that = this;
    let password = e.detail.value;

    that.setData({
      password: password
    })
  },

  /**
   * 输入确认密码
   */
  inputConfirmPassword: function(e) {
    let that = this;
    let confirm_password = e.detail.value;

    that.setData({
      confirm_password: confirm_password
    })
  },

  /**
   * 输入验证码
   */
  inputVerify: function(e) {
    let that = this;
    let verify_code = e.detail.value;

    that.setData({
      verify_code: verify_code
    })
  },

  /**
   * 输入手机号
   */
  inputMobile: function(e) {
    let that = this;
    let mobile = e.detail.value;

    that.setData({
      mobile: mobile
    })
  },

  /**
   * 输入动态验证码
   */
  inputOutCode: function(e) {
    let that = this;
    let out_code = e.detail.value;

    that.setData({
      out_code: out_code
    })
  },

  /**
   * 选择注册方式
   */
  registerType: function(e) {
    let that = this;
    let register_type = e.currentTarget.dataset.type;

    that.setData({
      register_type: register_type
    })
  },

  /**
   * 切换验证码
   */
  switchVerificationCode: function(e) {
    let that = this;
    app.verificationCode(that);
  },

  /**
   * 选中注册协议
   */
  checkProtocol: function() {
    let that = this;
    let is_read_protocol = that.data.is_read_protocol;

    is_read_protocol = is_read_protocol == 1 ? 0 : 1;
    that.setData({
      is_read_protocol: is_read_protocol
    })
  },

  /**
   * 获取动态码
   */
  outCode: function(e) {
    let that = this;

    let mobile = that.data.mobile;
    let verify_code = that.data.verify_code;
    let code_config = that.data.code_config;
    let key = app.globalData.key;
    let getOutCodeFlag = that.data.getOutCodeFlag;

    if (getOutCodeFlag == 1) {
      return false;
    }
    app.clicked(that, 'getOutCodeFlag');

    if (mobile == '') {
      app.showBox(that, '手机号不可为空');
      app.restStatus(that, 'getOutCodeFlag');
      return false;
    }

    if (!app.getRegex(mobile, 'mobile')) {
      app.showBox(that, '请输入正确的手机号');
      app.restStatus(that, 'getOutCodeFlag');
      return false;
    }

    if (code_config.value != undefined && code_config.value.pc == 1) {
      if (verify_code == '') {
        app.showBox(that, '验证码不可为空');
        app.restStatus(that, 'getOutCodeFlag');
        return false;
      }
      app.checkHasMobile(that, mobile, 'checkVerificationCode', true, 'getOutCodeFlag', 'getOutCode', verify_code);
    } else {
      app.checkHasMobile(that, mobile, 'getOutCodeFlag', true, 'getOutCodeFlag');
    }
  },

  /**
   * 获取动态验证码
   */
  getOutCode: function() {
    let that = this;
    let mobile = that.data.mobile;

    app.sendRequest({
      url: "System.Login.sendRegisterMobileCode",
      data: {
        mobile: mobile,
        key: app.globalData.openid,
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;

        if (code == 0) {
          if (data.code == 0) {
            let second = 120;
            let timer = setInterval(function() {
              second--;
              that.setData({
                second: second
              })
              if (second == 0) {
                clearInterval(timer);
              }
            }, 1000)
          }
          app.showBox(that, data.message);

          if (data.code < 0) {
            app.verificationCode(that);
          }

          app.restStatus(that, 'getOutCodeFlag');
        } else {
          app.restStatus(that, 'getOutCodeFlag');
        }
      }
    });
  },

  /**
   * 账号注册
   */
  register: function(e) {
    let that = this;
    let username = that.data.username;
    let password = that.data.password;
    let confirm_password = that.data.confirm_password;
    let verify_code = that.data.verify_code;
    let mobile = that.data.mobile;
    let out_code = that.data.out_code;
    let code_config = that.data.code_config;
    let reg_config = that.data.reg_config;
    let notice_config = that.data.notice_config;
    let register_type = that.data.register_type;
    let registerFlag = that.data.registerFlag;

    if (registerFlag == 1) {
      return false;
    }
    app.clicked(that, 'registerFlag');

    if (register_type == 1) {
      if (username == '') {
        app.showBox(that, '用户名不可为空');
        app.restStatus(that, 'registerFlag');
        return false;
      }

      if (password == '') {
        app.showBox(that, '密码不可为空');
        app.restStatus(that, 'registerFlag');
        return false;
      }

      if (password != confirm_password) {
        app.showBox(that, '两次密码输入不一致');
        app.restStatus(that, 'registerFlag');
        return false;
      }

      if (code_config.value != undefined) {
        if (code_config.value.pc == 1) {
          if (verify_code == '') {
            app.showBox(that, '验证码不可为空');
            app.restStatus(that, 'registerFlag');
            return false;
          }
        }
      }
    }

    if (register_type == 2) {
      if (mobile == '') {
        app.showBox(that, '手机号不可为空');
        app.restStatus(that, 'registerFlag');
        return false;
      }

      if (!app.getRegex(mobile, 'mobile')) {
        app.showBox(that, '请输入正确的手机号');
        app.restStatus(that, 'registerFlag');
        return false;
      }

      if (code_config.value != undefined) {
        if (code_config.value.pc == 1) {
          if (verify_code == '') {
            app.showBox(that, '验证码不可为空');
            app.restStatus(that, 'registerFlag');
            return false;
          }
        }
      }

      if (notice_config.noticeMobile == 1) {
        if (out_code == '') {
          app.showBox(that, '动态码不可为空');
          app.restStatus(that, 'registerFlag');
          return false;
        }
      }

      if (password == '') {
        app.showBox(that, '密码不可为空');
        app.restStatus(that, 'registerFlag');
        return false;
      }

      if (password != confirm_password) {
        app.showBox(that, '两次密码输入不一致');
        app.restStatus(that, 'registerFlag');
        return false;
      }
    }

    if (code_config.value != undefined && code_config.value.pc == 1) {
      let commit_type = notice_config.noticeMobile == 1 && register_type == 2 ? 'checkOutCode' : 'commit';
      app.checkVerificationCode(that, verify_code, commit_type, 'registerFlag'); // 检测图文验证码

    } else if (notice_config.noticeMobile == 1 && register_type == 2) {
      this.checkOutCode(); // 检测动态验证码

    } else {
      this.commit(); // 直接提交
    }
  },

  /**
   * 检测动态验证码
   */
  checkOutCode: function() {
    let mobile = this.data.mobile;
    let out_code = this.data.out_code;

    app.checkOutCode(this, out_code, 'mobile', mobile, 'registerFlag');
  },

  /**
   * 注册提交
   */
  commit: function() {
    let that = this;
    let username = that.data.username;
    let password = that.data.password;
    let mobile = that.data.mobile;
    let is_first_bind = app.globalData.is_first_bind;
    let source_uid = app.globalData.sourceid;

    let bind_message_info = '';
    if (is_first_bind == 1) {
      bind_message_info = app.globalData.bind_message_info;
    }

    let is_read_protocol = that.data.is_read_protocol;
    if (is_read_protocol == 0) {
      app.showBox(that, '请阅读并同意注册协议！');
      app.restStatus(that, 'registerFlag');
      return false;
    }

    app.sendRequest({
      url: 'System.Login.usernameRegister',
      data: {
        username: username,
        password: password,
        mobile: mobile,
        bind_message_info: bind_message_info,
        source_uid: source_uid
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;
        let message = res.message;
        console.log(res);
        if (code == 0) {
          app.restStatus(that, 'registerFlag');
          app.globalData.is_login = 1;
          app.globalData.is_logout = 0;
          app.globalData.token = data.token;
          app.globalData.is_first_bind = 0;
          app.globalData.bind_message_info = '';
          app.setSourceId('');
          if (!app.isNotLogin(1)) {
            wx.switchTab({
              url: '/pages/member/member/member',
            })
          }

        } else {
          app.showBox(that, message);
          app.restStatus(that, 'registerFlag');
        }
      }
    })
  }
})